/**
 * 
 */
package main;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.HashMap;
import java.util.LinkedList;

import org.json.JSONObject;

import data.Parser;
import data.ZugmonitorConnection;
import processing.core.PApplet;



import map.*;
/**
 * @author Mathias
 *
 */
@SuppressWarnings("serial")
public class Main extends PApplet{

	/**
	 * @param args
	 */    
	
	Map map;
	public static void main(String[] args) {
		PApplet.main(new String[] { PApplet.ARGS_PRESENT, "main.Main" });
	}
	
	 public void setup() {
		    size(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width,java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
		    
		    background(255);
		    //connect and parse data
		    
		    addMouseWheelListener(new MouseWheelListener() { 
		        public void mouseWheelMoved(MouseWheelEvent mwe) {
		          mouseWheel(mwe.getWheelRotation());
		      }
				});
		    
		    ZugmonitorConnection zgconn = new ZugmonitorConnection(ZugmonitorConnection.REQUEST.STATIONS);
			JSONObject json = zgconn.input;
			HashMap<Integer, Station> stations = Parser.parseStations(json);
			zgconn = new ZugmonitorConnection(ZugmonitorConnection.REQUEST.TRAINS);
			json = zgconn.input;
			HashMap<String, Train> trains = Parser.parseTrains(json);
			//create map
			map = new Map(stations, trains, this);
			
			Station a = map.stations.get(255);
			Station b = map.stations.get(256);
			//System.out.println("first arrival at destination: "+map.getTime(a, b));
			//load all icons
			//stations:
			for (Station s : map.stations.values()){
				s.loadIcon(this);
				
			}
			
			/*map.initList();*/
			Request.init(this, map);
			Scale.setStations(stations);
			this.requestFocus();
		  }

	 void mouseWheel(int delta) {
		 if(delta < 0) Controller.zoomIn(this);
		 else
			 Controller.zoomOut(this);
		 
		}
	  
	 
	public void draw() {  
		background(253,238,244);
		stroke(255, 0, 0);
		color(1,0,1);
		strokeWeight(3);
		
		Controller.calculate(this);
		Controller.translate(this);
		
		//draw map
		map.draw(this);
	}
	
	public void mousePressed(java.awt.event.MouseEvent e){
		super.mouseMoved();
		if (e.getButton()==MouseEvent.BUTTON1){
				Controller.mouseButtonDown(this);
				this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	}
	 
	public void mouseReleased(java.awt.event.MouseEvent e){
		super.mouseReleased();    
		if (e.getButton()==MouseEvent.BUTTON1){
			Controller.mouseButtonUp();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		}
	}
	
	public void keyPressed(){
		//System.out.println(keyCode);
		switch (keyCode){
		case 38:  Controller.scrollUp(); break;    // arrow up
		case 40:  Controller.scrollDown(); break;  // arrow down
		case 39:  Controller.scrollRight(); break; // arrow right
		case 37:  Controller.scrollLeft(); break;  // arrow left
		case 82:  Controller.scrollReset(this); break; // R
		case 107: Controller.zoomIn(this); break;      // +
		case 109: Controller.zoomOut(this); break;     // -
		case 90:  Controller.zoomReset(this); break;   // z
		case 32:  Controller.zoomReset(this); Controller.scrollReset(this); break; // space
		case 10:  Request.changeVisible(); break;     // enter
		default: break;
		}
	}

}
